/******************************************************************************
 * @brief: gp30 app
 * @Author: aple
 * @Date: 2022-12-14 08:40:58
 * @LastEditors: aple
 * @LastEditTime: 2022-12-14 14:24:05
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "mbapp.h"
#include "os_obj.h" // RTOS object definitions
#include "sys_init.h"
#include "common.h"
#include "bsp_api.h"
#include "dataproc.h"
#include "../com/com_obj.h"
#include "GP30_task.h"
#include "sys_task.h"

/******************************************************************************
 * @brief   micro.
 *****************************************************************************/

/******************************************************************************
 * @brief   var.
 *****************************************************************************/
gp30_st_type gp30_st;

/******************************************************************************
 * @brief   function.
 *****************************************************************************/
void gp30_task(const void *argv);
void gp30_event_send(uint16_t event);
void gp30_rt_com_handle(void);
void gp30_mirro_com_handle(void);

/******************************************************************************
 * @brief   gp30 task creat.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
obj_init_order5(gp30_task_creat);

void gp30_task_creat(void)
{
	osThreadDef(gp30_task, osPriorityNormal, 1, 0x400);
	os_obj.tid_gp30 = osThreadCreate(osThread(gp30_task), NULL);
}

/******************************************************************************
 * @brief   gp30 task.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void gp30_task(const void *argv)
{
	com03_Init(0, 2400, COM_PAR_EVEN);
	com08_Init(0, 2400, COM_PAR_EVEN);

	com_gp30.os_event_send = gp30_event_send;
	com_gp30.os_event_base = 0x200;
	com_gp30.rcv_end_time_sv = 120;
	
	com_mirro.os_event_send = gp30_event_send;
	com_mirro.os_event_base = 0x400;

	while (1)
	{
		osSignalWait(0, 1000);
		gp30_rt_com_handle();
		gp30_mirro_com_handle();
		if (gp30_st.cmd_req)
		{
			gp30_cmd_transmit(gp30_st.cmd_req);
			gp30_st.cmd_rsp = gp30_st.cmd_req;
			gp30_st.cmd_req = GP30_CMD_NONE;
		}
	}
}

/******************************************************************************
 * @brief   gp30 com2 handle.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void gp30_rt_com_handle(void)
{
	char buff[256];

	switch (com_gp30.rcv_st)
	{
	case COM_RX_RCVEND:
		// transmit data to com4
		if (gp30_st.mirro_req)
		{
			com_mirro.fun->snd_data(com_gp30.rx_buff, com_gp30.index_rx_rcv);
			gp30_st.mirro_req = 0;
		}

		hex2_to_ascii_multi_ext(com_gp30.rx_buff, buff, com_gp30.index_rx_rcv,""," ");
		dbg_print("RX[%02d]: %s\n", com_gp30.index_rx_rcv, buff);

		if (gp30_st.cmd_rsp != GP30_CMD_NONE)
		{
			gp30_cmd_rsp_handle(gp30_st.cmd_rsp, com_gp30.rx_buff, com_gp30.index_rx_rcv);
			gp30_st.cmd_rsp = GP30_CMD_NONE;
		}

		// clear rx state
		com_gp30.fun->rcv_st_clr();
		break;
	case COM_RX_TIMEOUT:
		// clear rx state
		com_gp30.fun->rcv_st_clr();
		break;
	default:
		break;
	}
}

/******************************************************************************
 * @brief   gp30 com4 handle.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void gp30_mirro_com_handle(void)
{
	char buff[256];

	switch (com_mirro.rcv_st)
	{
	case COM_RX_RCVEND:
		// transmit data to com2
		com_gp30.fun->snd_data(com_mirro.rx_buff, com_mirro.index_rx_rcv);

		dbg_puts(SYMBOL_LINE_EQ PTCR);
		hex2_to_ascii_multi_ext(com_mirro.rx_buff, buff, com_mirro.index_rx_rcv ,""," ");
		dbg_print("TX[%02d]: %s\n", com_mirro.index_rx_rcv, buff);

		// clear rx state
		com_mirro.fun->rcv_st_clr();

		gp30_st.mirro_req = 1;
		break;
	case COM_RX_TIMEOUT:
		// clear rx state
		com_mirro.fun->rcv_st_clr();
		break;
	default:
		break;
	}
}

/******************************************************************************
 * @brief   gp30 event task.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void gp30_event_send(uint16_t event)
{
	osSignalSet(os_obj.tid_gp30, event);
}

/******************************************************************************
 * @brief   gp30 common cmd.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
int gp30_cmd(int argc, char *argv[])
{
	uu08 buff[128], len;
	char str[128];

	dbg_puts(SYMBOL_LINE_EQ PTCR);

	if (argc > 1)
	{
		// cmd convert and send
		len = ascii_to_hex2_multi(argv[1], buff, 128);
		com_gp30.fun->snd_data(buff, len);

		// print tx infomation
		hex2_to_ascii_multi_ext(buff, str, len, ""," ");
		dbg_print("TX[%02d]: %s\n", len, str);
	}
	else
	{
		dbg_puts("gp30 cmd exe : arg is few!\n");
	}

	return 0;
}
SHELL_FUN_EXPORT(gp30_cmd);

/******************************************************************************
 * @brief   gp30 read cmd.
 * @param   argv
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
int gp30_mread(int argc, char *argv[])
{
	uu08 cmd_ok = 0;

	dbg_puts(SYMBOL_LINE_EQ PTCR);

	if (argc > 1)
	{
		// read cmd version 2208
		if (comp_str(argv[1], "2208", 4) == 0)
		{
			gp30_st.cmd_req = GP30_CMD_READ_2208;
			osSignalSet(os_obj.tid_gp30, 0x801);
			cmd_ok = 1;
		}
		// read cmd version 2301
		else if (comp_str(argv[1], "2301", 4) == 0)
		{
			gp30_st.cmd_req = GP30_CMD_READ_2301;
			osSignalSet(os_obj.tid_gp30, 0x801);
			cmd_ok = 1;
		}
		// read cmd illegal
		else
		{
			dbg_puts("gp30 read err : arg is illegal!\n");
			dbg_puts("arg opt: 2208 | 2301\n");
		}
	}
	else
	{
		dbg_puts("gp30 read exe : arg is few!\n");
	}

	return 0;
}
SHELL_FUN_EXPORT(gp30_mread);
